﻿using System;
using System.AddIn;
using System.Collections.Generic;
using AddInActorView;

namespace dotSimpl.wlanner.AddIns.NetworkSettings
{
    /// <summary>
    /// Add-in for changing network settings.
    /// </summary>
    [AddIn("Network Settings Changer", Version = "0.1")]
    public class NetworkSettingsChanger : ActorView
    {
        #region Constants

        //settings names
        private const string enableDHCP = "Enable DHCP";
        private const string ipAddress = "IP Address";
        private const string subnetMask = "Subnet Mask";
        private const string gateway = "Gateway";
        private const string dns = "DNS Servers";

        #endregion

        #region Properties

        /// <summary>
        /// Gets the name of the add-in.
        /// </summary>
        public override string InternalName
        {
            get { return this.ToString(); }
        }

        /// <summary>
        /// Gets add-in compatibility version.
        /// </summary>
        public override string CompatibilityVersion
        {
            get { return "v0.0.1"; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets initial settings for UI.
        /// </summary>
        public override KeyValuePair<string, string>[] GetInitialSettins()
        {
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            result.Add(new KeyValuePair<string, string>(enableDHCP, true.ToString()));
            result.Add(new KeyValuePair<string, string>(ipAddress, string.Empty));
            result.Add(new KeyValuePair<string, string>(subnetMask, string.Empty));
            result.Add(new KeyValuePair<string, string>(gateway, string.Empty));
            result.Add(new KeyValuePair<string, string>(dns, string.Empty));
            return result.ToArray();
        }

        /// <summary>
        /// Applies settings to system network connections.
        /// </summary>
        public override void ApplySettings(KeyValuePair<string, string>[] settings, Guid networkCard)
        {
            Dictionary<string, string> s = new Dictionary<string, string>();
            for (int i = 0; i < settings.Length; i++)
            {
                s.Add(settings[i].Key, settings[i].Value);
            }

            if (!bool.Parse(s[enableDHCP]))
            {
                string ip = s[ipAddress];
                string subnet = s[subnetMask];
                string gw = s[gateway];
                string DNS = s[dns];
                NetworkInterfaceSettingsHelper.SetIP(networkCard, ip, subnet, gw, DNS);
            }
            else
            {
                NetworkInterfaceSettingsHelper.SetDHCP(networkCard);
            }
        }

        public override string[] GetSettingNames()
        {
            return new string[] { enableDHCP, ipAddress, subnetMask, gateway, dns };
        }

        #endregion
    }
}
